﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.Common;
using MySql.Data.Types;
using MySql.Data.MySqlClient;

namespace sistem.Korisnicke_kontrole
{
    public partial class OcjenaKontrola : UserControl
    {
        Korisnik korisnik;
        MySqlDataAdapter da = new MySqlDataAdapter();
        DataTable dt = new DataTable();

        public OcjenaKontrola()
        {
            InitializeComponent();
        }
        public OcjenaKontrola(Korisnik k, int ucenik_id, int predmet_id)
        {
            InitializeComponent();
            korisnik = k;
            if (korisnik is Ucenik || korisnik is Pedagog)
            {
                dataGridView1.AllowUserToAddRows = false;
                dataGridView1.ReadOnly = true;
            }
            string constring = "server=localhost;user=root;pwd=" + "" + ";database=skola";
            MySqlConnection con = new MySqlConnection(constring);
            MySqlCommand cmd = new MySqlCommand("SELECT ou.id, c.id, c.pocetak, ou.vrijednost, ou.dodatne_napomene FROM ocjena_ucenik ou, cas c WHERE ou.ucenik_id=@UCENIK and ou.cas_id=c.id and c.predmet_id=@PREDMET;", con);
            cmd.Parameters.AddWithValue("@UCENIK", ucenik_id);
            cmd.Parameters.AddWithValue("@PREDMET", predmet_id);
            cmd.CommandType = CommandType.Text;
            da = new MySqlDataAdapter(cmd);
            da.Fill(dt);
            dataGridView1.DataSource = dt;
            dataGridView1.Columns[0].Visible = false;
            dataGridView1.Columns[1].Visible = false;
        }

        private void dataGridView1_CellMouseLeave(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                MySqlCommandBuilder cb = new MySqlCommandBuilder(da);
                da.Update(dt);
            }
            catch(Exception Ex)
            {
                MessageBox.Show(Ex.Message);
            }
        }
    }
}
